# -*- coding: utf-8 -*-
# filename: handle.py


import json
import re
from lottery.view import mysql_util

def signup(request):
    try:
        webData = json.loads(request.body)
        name = webData['name']
        mobile = webData['mobile']
        className = webData['className']
        smsCode = webData['smsCode']
        message = webData['message']
        pattern1 = re.compile(r'1[0-9]{10}')
        pattern2 = re.compile(u"^[\u4E00-\u9FA5]+$")
        if not pattern1.match(mobile):
            result = {'result': False,'msg':'手机号错误'}
        elif not pattern2.match(name):
            result = {'result': False,'msg':'姓名错误，请输入您的真实姓名'}
        else:
            param = (mobile)
            conn = mysql_util.getconn()
            cr = conn.execute('select smscode from checks where mobile=%s', param)
            res = conn.fetchall()
            if cr > 0 and res[0][0] == smsCode:
                param = (mobile)
                sr = conn.execute('select * from sign where mobile=%s', param)
                if sr == 0:
                    param = (name,mobile,className,message)
                    conn.execute('insert into sign(name,mobile,className,message) values(%s,%s,%s,%s)', param)
                    result = {'result':True}
                    return json.dumps(result)
                else:
                    result = {'result': False, 'msg': '该手机签到已完成，请勿重复签到'}
            else:
                result = {'result': False,'msg':'验证码错误'}
            conn.close()
        return json.dumps(result)
    except Exception as e:
        return e.args